package com.github.ilioili.justdoit.components.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.ilioili.justdoit.R;
import com.github.ilioili.justdoit.common.AppBaseActivity;
import com.github.ilioili.justdoit.common.config.UserInfo;
import com.github.ilioili.justdoit.model.dao.ScheduleEventDao;
import com.github.ilioili.justdoit.util.sql.SqlCallback;
import com.github.ilioili.justdoit.widget.AxisChart;
import com.github.ilioili.qqlogin.QqLoginBridgeActivity;
import com.taihe.template.base.injection.Id;
import com.taihe.template.base.injection.Layout;

import ht.util.img.Img;

@Layout(R.layout.activity_profile)
public class ProfileActivity extends AppBaseActivity {

    @Id(R.id.iv_icon)
    private ImageView ivIcon;
    @Id(R.id.tv_name)
    private TextView tvName;
    @Id(R.id.tv_total_amount)
    private TextView tvTotalAmount;
    @Id(R.id.tv_total_time)
    private TextView tvTotalTime;
    @Id(R.id.chart)
    private AxisChart chart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScheduleEventDao.getCompleteCount(new SqlCallback<Integer>() {
            @Override
            public void onSucess(Integer integer) {
                tvTotalAmount.setText(getString(R.string.accumulate_complete) + integer + getString(R.string.unit_tasks));
            }
        });
        ScheduleEventDao.getCompleteTotalTime(new SqlCallback<Integer>() {
            @Override
            public void onSucess(Integer integer) {
                int day = 24 * 60;
                tvTotalTime.setText(getString(R.string.accululate_time_spend) + integer / day + getString(R.string.unit_day) + integer % day / 60 + getString(R.string.unit_hour) + integer % day % 60 + getString(R.string.unit_minute));
            }
        });
        ScheduleEventDao.getRecentHalfMonthData(new SqlCallback<int[]>() {

            @Override
            public void onSucess(int[] data) {
                chart.setData(data);
            }
        });
        tvName.setText(UserInfo.getName());
        Img img = new Img(getCacheDir(), getFilesDir(), UserInfo.getIconUrl());
        img.setRadius(50);
        img.setImageViewAsync(ivIcon);
    }

    public static Intent newIntent(Context context) {
        return new Intent(context, ProfileActivity.class);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    public void onClickExit(View v) {
        UserInfo.clear();
        finish();
    }
}
